package com.njtoyo.taxi.admin.service.open.impl;

import com.njtoyo.taxi.admin.rest.wrapper.open.CoordinatesWrapper;
import com.njtoyo.taxi.admin.service.open.ToolService;
import com.taxi.entity.common.RestResult;
import com.vividsolutions.jts.geom.Coordinate;
import org.springframework.stereotype.Service;

@Service
public class ToolServiceImpl implements ToolService {
    /**
     * 计算距离
     *
     * @param
     * @return
     */
    @Override
    public RestResult coordinates(CoordinatesWrapper wrapper) {
        String[] destination = wrapper.getDestination().split(",");
        String[] currentDriverLocates = wrapper.getCurrentDriverLocate().split(",");
        //经度 x
        double longitude = Double.parseDouble(destination[0]);
        double longitude1 = Double.parseDouble(currentDriverLocates[0]);
        //纬度 y
        double latitude = Double.parseDouble(destination[1]);
        double latitude1 = Double.parseDouble(currentDriverLocates[1]);

        Coordinate origin = new Coordinate();
        origin.x = longitude1;
        origin.y = latitude1;

        Coordinate dest = new Coordinate();
        dest.x = longitude;
        dest.y = latitude;

        double distance = distance(origin, dest);
        //千米转换米
        double distances = distance * 1000;
        return RestResult.success(distances);
    }

    /**
     * 计算距离公式
     *
     * @param origin
     * @param dest
     * @return
     */
    public static double distance(Coordinate origin, Coordinate dest) {

        double lon1 = (Math.PI / 180) * origin.x;
        double lon2 = (Math.PI / 180) * dest.x;
        double lat1 = (Math.PI / 180) * origin.y;
        double lat2 = (Math.PI / 180) * dest.y;

        // 地球半径
        double R = 6371.393;
        // 两点间距离 km，如果想要米的话，结果*1000就可以了
        double d1 = R * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon1));
        double d2 = R * Math.acos(Math.sin(lat2) * Math.sin(lat2) + Math.cos(lat2) * Math.cos(lat2) * Math.cos(lon2 - lon1));
        return (d1 + d2);
    }
}
